//---------------------------------------------------------------------------
// MyManTest
// Author : Alex Woo-Hyun Kim at www.webrobot.org
// Email : woorung@webrobot.org,webrobot@hotmail.com,woorung@empal.com
// Date : Apr. 17, 2002
//---------------------------------------------------------------------------
#include <stdio.h>
#include <iostream>
#include "CMyMan.h"
#include "CMyStatement.h"
#include "CMyResultSet.h"
//---------------------------------------------------------------------------
int main(int argc,char *argv[])
{
	CMyMan *MyM = new CMyMan("MyConMan.conf");
	CMyResultSet *MyRS = MyM->GetMyResultSet("show databases");

	// print all of field names
    while(MyRS->HasMoreMetaNameFromMyResultSet()) {
        printf("%15s",(char *) MyRS->GetMetaNameString());
    }
    printf("\n---------------------------------------------------------------\n");

    // print all of columns
    while(MyRS->HasMoreRowsFromMyResultSet()) {
        while(MyRS->HasMoreColumnFromCurRows()) {
            printf("%13s",(char *) MyRS->GetString());
        }
        printf("\n");
    }

	/*
	while(MyRS->HasMoreRowsFromMyResultSet()) {
		char *pszUrl = (char *) MyRS->GetString("docid");
		char *pszTitle = (char *) MyRS->GetString("title");
		cout << pszUrl << endl;
		cout << pszTitle << endl;
	}
	/*
	delete MyRS;
	delete MyM;
	return 0;

// This is a sample for CMyStatement
// ---------------------------------
/*
	CMyStatement *MyStatement = new CMyStatement("select * from (?) where (?) = (?) and no = (?) or (?) = \'nara\'","(?)");
	//CMyStatement *MyStatement = new CMyStatement("select * from ? where ? = ? and no = ? or ? = \'nara\'");
	MyStatement->SetName(1,"url_content");
	MyStatement->SetName(2,"name");
	MyStatement->SetString(3,"woorung");
	MyStatement->SetInteger(4,3);
	MyStatement->SetName(5,"alphabet");
//	MyStatement->SetString(6,"woorungnara");
	cout << "result : [" << MyStatement->GetStatement() << "]" << endl;
	delete MyStatement;
	return 0;
*/
	if(argc<2) {
		cout << "usages : CMyManTest <table_name>" << endl;
		return -1;	
	}

	CMyMan *MyMan = new CMyMan("MyConMan.conf");

	CMyStatement *MyStatement = new CMyStatement("select * from ?");
	MyStatement->SetName(1,argv[1]);

	//CMyResultSet *MyResultSet = MyMan->GetMyResultSet("describe category_code");
	//CMyResultSet *MyResultSet = MyMan->GetMyResultSet("select * from service_code");
	CMyResultSet *MyResultSet = MyMan->GetMyResultSet(MyStatement);

	delete MyStatement;


	FILE *f = fopen("__IDXFILE","a+");
	
	while(MyResultSet->HasMoreRowsFromMyResultSet()) {
		while(MyResultSet->HasMoreColumnFromCurRows()) {
			fprintf(f,"%15s\t",(char *) MyResultSet->GetString("url"));
			fprintf(f,"%15s\t",(char *) MyResultSet->GetString("title"));
			fprintf(f,"%10s\t",(char *) MyResultSet->GetString("bizphone"));
			fprintf(f,"%20s\t",(char *) MyResultSet->GetString("naddr"));
			fprintf(f,"%30s\t",(char *) MyResultSet->GetString("description"));
			fprintf(f,"%10s\t",(char *) MyResultSet->GetString("scode"));
			fprintf(f,"%30s\t",(char *) MyResultSet->GetString("keyword"));
			fprintf(f,"%30s\t",(char *) MyResultSet->GetString("class1"));
			fprintf(f,"\n");
		}
	}

	fclose(f);
	delete MyResultSet;





// when you wanna deal with each column manually, you can code as the following.
/*
	printf("%15s",(char *) MyResultSet->GetMetaNameString(0));
	printf("%15s",(char *) MyResultSet->GetMetaNameString(1));
	printf("%15s",(char *) MyResultSet->GetMetaNameString(2));
	printf("\n");
	printf("----------------------------------------------------------------\n");
	while(MyResultSet->HasMoreRowsFromMyResultSet()) {
		printf("%15s",(char *) MyResultSet->GetString(0));
		printf("%15s",(char *) MyResultSet->GetString(1));
		printf("%15s",(char *) MyResultSet->GetString(2));
		printf("\n");
	}
*/

// when you wanna deal with all of columns in result set, you can code like this.
/*
	// print all of field names
	while(MyResultSet->HasMoreMetaNameFromMyResultSet()) {
		printf("%15s",(char *) MyResultSet->GetMetaNameString());
	}
	printf("\n---------------------------------------------------------------\n");
	
	// print all of columns
	while(MyResultSet->HasMoreRowsFromMyResultSet()) {
		while(MyResultSet->HasMoreColumnFromCurRows()) {
			printf("%13s",(char *) MyResultSet->GetString());
		}
		printf("\n");
	}

	delete MyResultSet;	
	delete MyMan;	
*/
}
